//
//  me_chorus_D0.m
//  K-Music
//
//  Created by CHUN on 16/1/15.
//  Copyright © 2016年 CHUN. All rights reserved.
//

#import "me_chorus_D0.h"
#import "GroupModel.h"

@interface me_chorus_D0 ()<UITableViewDataSource ,UITableViewDelegate>
{
    GroupModel * mode;
}
@property (nonatomic ,strong) NSMutableArray * Data;
@property (nonatomic ,strong) UITableView * me_chorus_TableView;

@end

@implementation me_chorus_D0

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = RGB(241, 241, 241);
    self.navigationItem.title = @"我发起的合唱";
    
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithIcon:@"back" highIcon:@"back" target:self action:@selector(back_back)];
    
    mode = [[GroupModel alloc]init];
    
    [self get_Data];
    
    [self creatUI];
}

- (void)back_back
{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark -creatUI-
- (void)creatUI
{
    _me_chorus_TableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT+49) style:UITableViewStylePlain];
    _me_chorus_TableView.delegate = self;
    _me_chorus_TableView.dataSource =self;
    _me_chorus_TableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _me_chorus_TableView.backgroundColor = RGB(241, 241, 241);
    [self.view addSubview:_me_chorus_TableView];
    
}

- (void)get_Data
{
    [MBProgressHUD showWithStatus:@"加载中..." toView:self.view];
    
    AFHTTPRequestOperationManager * manager = [AFHTTPRequestOperationManager manager];
    
    NSString*infoStr11=[NSString stringWithFormat:@"/api/chorus/my_chorus?page=0&size=20&sort=createdDate,DESC"];
    
    NSString *intactURL = [MyBaseUrl stringByAppendingString:infoStr11];
    
    NSString * token = [FileStorage NSUserDefault_getObjectForKey:access_token];
    
    //    创建请求
    manager.requestSerializer =[AFHTTPRequestSerializer serializer];
    
    [manager.requestSerializer setValue:@"application/json"
                     forHTTPHeaderField:@"Accept"];
    [manager.requestSerializer setValue:[NSString stringWithFormat: @"Bearer %@",token]
                     forHTTPHeaderField:@"Authorization"];
    
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    [manager GET:intactURL parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSDictionary * dic = [NSJSONSerialization JSONObjectWithData:responseObject options:0 error:nil];
        
        NSLog(@"成功GET --> %@", dic);
        _Data = [NSMutableArray array];
        
        NSArray * arr = dic[@"body"][@"content"];
        NSArray * Datas = [GroupModel objectArrayWithKeyValuesArray:arr];
        _Data = [NSMutableArray arrayWithArray:Datas];
        [self.me_chorus_TableView reloadData];
        
        
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        NSLog(@"%@",error);
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        [MBProgressHUD showErrorWithStatus:@"数据获取失败" toView:self.view];
    }];
}

#pragma mark -UITableViewDataSource-

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _Data.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 80;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    static NSString *cellID = @"cellID";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    
    GroupModel * data = [self.Data objectAtIndex:indexPath.row];
    
    if (cell == nil) {
        
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellID];
        UIImageView * icon = [[UIImageView alloc]initWithFrame:CGRectMake(50, 10, 60, 60)];
        
        [icon sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",data.avatar]]];
        icon.layer.cornerRadius= 30;
        icon.layer.masksToBounds = YES;
        
        [cell.contentView addSubview:icon];
        
        UILabel * mylabel = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(icon.frame)+30, 10, WIDTH/2, 30)];
        mylabel.text=[NSString stringWithFormat:@"%@",data.nickName];
        mylabel.textColor=[UIColor orangeColor];
        mylabel.textAlignment=NSTextAlignmentLeft;
        [cell.contentView addSubview: mylabel];
        
        UILabel * name = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(icon.frame)+30, 40, WIDTH/4, 30)];
        name.text = [NSString stringWithFormat:@"%@ 个合唱",data.songId];
        name.textColor=[UIColor grayColor];
        name.textAlignment=NSTextAlignmentLeft;
        [cell.contentView addSubview: name];
        
        UILabel * totalPlay = [[UILabel alloc]initWithFrame:CGRectMake(10,25, 30, 30)];
        totalPlay.text = [NSString stringWithFormat:@"%ld. ",indexPath.row +1];
        totalPlay.textColor=[UIColor grayColor];
        totalPlay.font = [UIFont boldSystemFontOfSize:20];
        totalPlay.textAlignment=1;
        [cell.contentView addSubview: totalPlay];
        
    }
    
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:tableView.indexPathForSelectedRow animated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
